package top.gbiot.device.controller;

import java.util.*;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.gbiot.common.annotation.Log;
import top.gbiot.common.core.controller.BaseController;
import top.gbiot.common.core.domain.AjaxResult;
import top.gbiot.common.core.domain.entity.SysUser;
import top.gbiot.common.core.page.TableDataInfo;
import top.gbiot.common.domain.DataMap;
import top.gbiot.common.enums.BusinessType;
import top.gbiot.common.utils.ShiroUtils;
import top.gbiot.common.utils.poi.ExcelUtil;
import top.gbiot.device.domain.IotDevice;
import top.gbiot.device.service.IIotDeviceService;
import top.gbiot.project.domain.IotProject;
import top.gbiot.project.service.IIotProjectService;
import top.gbiot.redis.mapper.RedisMapper;
import top.gbiot.redis.util.RedisUtils;

/**
 * 设备管理Controller
 * 
 * @author gb
 * @date 2021-02-13
 */
@Controller
@RequestMapping("/iot/device")
public class IotDeviceController extends BaseController
{
    private String prefix = "iot/device";

    protected   final Logger log= LoggerFactory.getLogger(getClass());

    @Autowired
    private IIotDeviceService iotDeviceService;

    @Autowired
    private IIotProjectService iotProjectService;

    @Autowired
    private RedisMapper redisMapper;

    @Autowired
    private RedisUtils redisUtils;

    @RequiresPermissions("iot:device:view")
    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }

    /**
     * 查询设备管理列表
     */
    @RequiresPermissions("iot:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IotDevice iotDevice)
    {
        startPage();
        List<IotDevice> list = iotDeviceService.selectIotDeviceList(iotDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @RequiresPermissions("iot:device:export")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IotDevice iotDevice)
    {
        List<IotDevice> list = iotDeviceService.selectIotDeviceList(iotDevice);
        ExcelUtil<IotDevice> util = new ExcelUtil<IotDevice>(IotDevice.class);
        return util.exportExcel(list, "device");
    }

    /**
     * 新增设备管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备管理
     */
    @RequiresPermissions("iot:device:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IotDevice iotDevice)
    {
        iotDevice.setDeviceId(UUID.randomUUID().toString().replaceAll("-",""));
        // 获取当前的用户信息
        SysUser currentUser = ShiroUtils.getSysUser();
        iotDevice.setCreator(currentUser.getUserId().toString());
        iotDevice.setCreateDate(new Date());
        int resultRow =iotDeviceService.insertIotDevice(iotDevice);
        List<DataMap> deviceCacheList = redisMapper.selectAllDevice4Cache();
        boolean deviceResult=redisUtils.set("deviceCache", JSON.toJSONString(deviceCacheList));
        if(deviceResult){
            log.info("加载设备缓存信息成功！");
        }else{
            log.warn("加载设备缓存信息失败！");
        }
        return toAjax(resultRow);
    }

    /**
     * 修改设备管理
     */
    @GetMapping("/edit/{deviceId}")
    public String edit(@PathVariable("deviceId") String deviceId, ModelMap mmap)
    {
        IotDevice iotDevice = iotDeviceService.selectIotDeviceById(deviceId);
        mmap.put("iotDevice", iotDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备管理
     */
    @RequiresPermissions("iot:device:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IotDevice iotDevice)
    {
        int reslutRow=iotDeviceService.updateIotDevice(iotDevice);
        List<DataMap> deviceCacheList = redisMapper.selectAllDevice4Cache();
        boolean deviceResult=redisUtils.set("deviceCache", JSON.toJSONString(deviceCacheList));
        if(deviceResult){
            log.info("加载设备缓存信息成功！");
        }else{
            log.warn("加载设备缓存信息失败！");
        }
        return toAjax(reslutRow);
    }

    /**
     * 删除设备管理
     */
    @RequiresPermissions("iot:device:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int reslutRow=iotDeviceService.deleteIotDeviceByIds(ids);
        List<DataMap> deviceCacheList = redisMapper.selectAllDevice4Cache();
        boolean deviceResult=redisUtils.set("deviceCache", JSON.toJSONString(deviceCacheList));
        if(deviceResult){
            log.info("加载设备缓存信息成功！");
        }else{
            log.warn("加载设备缓存信息失败！");
        }
        return toAjax(reslutRow);
    }


    /**
     * 查询项目列表集合用于动态下拉列表
     * @param
     * @return
     */
    @GetMapping("/getAllProjectList")
    @ResponseBody
    public Map<String,Object> getAllDeviceType(){
        List<IotProject> projectList = iotProjectService.getAllProjectList();
        Map<String,Object> map=new HashMap<>();
        map.put("projectList",projectList);
        return map;
    }
}
